<!DOCTYPE html>
<html class="x-admin-sm" lang="en" xmlns:th="https://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>定时任务管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" th:href="@{/assets/admin/css/font.css}">
    <link rel="stylesheet" th:href="@{/assets/admin/css/xadmin.css}">
    <script th:src="@{/assets/admin/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/assets/admin/js/xadmin.js}"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a href="">演示</a>
                <a>
                    <cite>导航元素</cite></a>
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form" action="" lay-filter="query_quartz_form">
                        <div class="layui-inline layui-show-xs-block">
                            <input class="layui-input" autocomplete="off" placeholder="开始日" name="start" id="start">
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <input class="layui-input" autocomplete="off" placeholder="截止日" name="end" id="end"></div>
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" name="jobName" placeholder="请输入用户名" autocomplete="off"
                                   class="layui-input" id="jobName"></div>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" lay-submit lay-filter="sreach">
                                <i class="layui-icon">&#xe615;</i>查询</button>
                        </div>
                    </form>
                </div>
                <div class="layui-card-body ">
                    <button class="layui-btn" onclick="openAddQuartz()">
                        <i class="layui-icon">&#xe642;</i>新增任务
                    </button>
                    <table class="layui-table" lay-filter="quartz-table" id="table1">
                        <thead>
                        </thead>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script th:src="@{/assets/admin/js/jquery.min.js}"></script>
<script th:replace="systemCommon/HarinCommon :: commonJs"></script>
<script type="text/html" id="switchStatus">
    <!--这里的checked的状态只是演示-->
    <input type = "checkbox" name = "status" value = "{{ d.jobId }}" data-type="" lay-skin = "switch"lay-text = "正常|停用" lay-filter = "switchTest" {{ d.status == 1 ? 'checked':'' }}>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script>
    let quartzTable = null;
    $(function () {
        layui.use(['jquery','laydate', 'table', 'form'],
            function () {
                laydate = layui.laydate;
                table = layui.table;
                form = layui.form;
                $ = layui.jquery;

                table.on('edit(quartz-table)', function (obj) { //里面的值是table的lay-filter
                    let value = obj.value; //得到修改后的值
                    data = obj.data; //得到所在行所有键值
                    field = obj.field; //得到字段
                    layer.msg('[' + field + ': ' + data.id + '] ' + field + ' 字段更改为：' + value);
                    //失去焦点
                    $(this).blur();
                });

                //执行一个laydate实例
                laydate.render({
                    elem: '#start' //指定元素
                });

                //执行一个laydate实例
                laydate.render({
                    elem: '#end' //指定元素
                });

                //第一个实例
                quartzTable = table.render({
                    elem: '#table1',
                    url: '/task/getTaskList', //数据接口
                    method: "get",
                    where: {     //请求参数
                        "s_date": $("#start").val(),
                        "e_date": $("#end").val(),
                        "jobName":$("#jobName").val()
                    },
                    parseData: function (res) { //res 即为原始返回的数据
                        var data = res.result;
                        //console.log(data.records);
                        //console.log(typeof data.records);
                        return {
                            "code": res.code, //解析接口状态
                            // "msg": res.message, //解析提示文本
                            "count": data.total, //解析数据长度
                            "data": data.records //解析数据列表
                        };
                    },
                    response: {//重新定义规则
                        //statusName: 'status', //规定数据状态的字段名称，默认：code
                        statusCode: 200, //规定成功的状态码，默认：0
                        //msgName: 'hint', //规定状态信息的字段名称，默认：msg
                        //countName: 'total', //规定数据总数的字段名称，默认：count
                        //dataName: 'rows' //规定数据列表的字段名称，默认：data
                    },
                    page: true, //开启分页
                    limits: [10, 20, 50],//可选条目数
                    toolbar: true,//若需要“列显示隐藏”、“导出”、“打印”等功能，则必须开启该参数
                    loading: true, //加载动画
                    cols: [
                        [ //表头
                            {type: 'checkbox', fixed: 'left'},
                            {field: 'jobId', title: 'ID', sort: true, fixed: 'left', width: 100},
                            {field: 'jobName', title: '任务名', width:150},
                            {field: 'beanName', title: '实体类名',width:200},
                            {field: 'methodName', title: '方法名',width:120},
                            {field: 'cron', title: '表达式', edit: 'text',width:120},
                            {field: 'status', title: '状态', templet: '#switchStatus', width: 95,unresize: true},
                            {field: 'jobGroup', title: '任务分组', sort: true,width: 100,},
                            {field: 'createTime', title: '创建时间',width:145,sort: true,templet : "<div>{{layui.util.toDateString(d.createTime, 'yyyy年MM月dd日 HH:mm:ss')}}</div>"},
                            {field: 'updateTime', title: '更新时间',width: 145,templet : "<div>{{layui.util.toDateString(d.createTime, 'yyyy年MM月dd日 HH:mm:ss')}}</div>"},
                            {field: 'operation', title:'操作', toolbar: '#barDemo',fixed: 'right',unresize: true,width: 160}
                        ]
                    ]
                });
                //必须使用单引号。过分坑啊！！！！
                form.on('submit(sreach)', function (data) {
                    quartzTable.reload({
                        where: {
                            "s_date": data.field.start,
                            "e_date": data.field.end,
                            "jobName": data.field.jobName
                        },
                        page:{
                            curr:1
                        }
                    });
                    return false;
                });
                //开关选择
                form.on('switch(switchTest)', function(data){
                    let jobId = data.value;
                    let status = "0";
                    if(this.checked){
                        status ="1";
                    }else{
                        status ="0";
                    }
                    // layer.tips('开关checked：'+ (this.checked ? 'true' : 'false'), data.othis)
                    $.ajax({
                        url: "/task/updateJobStatus",
                        type:"get",
                        data:{
                            "jobId":jobId,
                            "status":status
                        },
                        success:function(data){
                            if (data.code == 200) {
                                layer.alert(data.message, {icon: 6}, function (iIndex) {
                                    layer.close(iIndex);
                                });
                            } else {
                                layer.alert(data.message, {icon: 5}, function (iIndex) {
                                    layer.close(iIndex);
                                });
                            }
                        }

                    })
                });
                //监听行工具事件  这个quartz-table是指，<table> 属性lay-filter = 'quartz-table'
                table.on('tool(quartz-table)', function (obj) {
                    //当前行的数据
                    let data = obj.data;
                    console.log(data);
                    //console.log(data);
                    //删除
                    if(obj.event === "del"){
                        layer.confirm("确定删除吗？",function(index){
                            $.ajax({
                                url: "/task/deleteTaskById",
                                type: "DELETE",
                                data: {"jobId": data.jobId},
                                //dataType: "json",
                                success: function (data) {
                                    refreshData();
                                    if (data.code == 200) {
                                        layer.alert(data.message, {icon: 6}, function (iIndex) {
                                            layer.close(iIndex);
                                        });
                                    } else {
                                        layer.alert(data.message, {icon: 5}, function (iIndex) {
                                            layer.close(iIndex);
                                        });
                                    }
                                }
                            });
                            layer.close(index);
                        });
                    }//编辑
                    else if(obj.event === "edit"){
                        openLayer("编辑定时任务信息","/system/quartzEditUI?taskId="+data.jobId,null,null,false,refreshData());
                    }
                });
            })
    });
    function refreshData(page) {
        if(isEmpty(page)){
            page = $(".layui-laypage-em").next().html();
        }
        //获取form表单的数据，query_quartz_form 是 <form lay-filter="query_quartz_form">
        var data = form.val("query_quartz_form");
        quartzTable.reload({
            page: {
                curr: page  //主要代码行
            },
            where: {
                "s_date": data.start,
                "e_date": data.end,
                "jobName": data.jobName
            }
        });
    }
    function openAddQuartz(){
        openLayer('新增定时任务','/system/quartzEditUI',null,null,false,refreshData());
    }
</script>
</html>